package com.menhai.personmanagement.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.menhai.personmanagement.entity.MbFollowupVisit;
import com.menhai.personmanagement.mapper.MbFollowupVisitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.menhai.personmanagement.utils.DateUtil.subOrAddMinut;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author shikun
 * @since 2021-12-27
 */
 @Service
public class IMbFollowupVisitService{

    @Autowired
    private MbFollowupVisitMapper mbFollowupVisitMapper;

    public int insertVisit(MbFollowupVisit mbFollowupVisit){
        return mbFollowupVisitMapper.insert(mbFollowupVisit);
    }

    public List<MbFollowupVisit> getVisit(String id,String type,String hosnumarray){
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("userId",id);
//        queryWrapper.eq("ChronicType", type);
//        queryWrapper.orderByAsc("date");
        List list = new ArrayList();
        if(!StringUtils.isEmpty(hosnumarray)){
            list = Arrays.stream(hosnumarray.split(",")).collect(Collectors.toList());
        }
        return mbFollowupVisitMapper.getVisit(Integer.parseInt(id), type, list);
    }

    /**
     * @describe 首页日历
     * @param id
     * @Return java.util.List<com.menhai.personmanagement.entity.MbFollowupVisit>
     * @Date 2021/12/31 16:08
     * @author shikun
     */
    public List<MbFollowupVisit> getcalendarVisit(String id){
        return mbFollowupVisitMapper.getcalendarVisit(id);
    }

    /**
     * @describe 首页日历 提示dialog
     * @param id
     * @param date
     * @Return java.util.List<com.menhai.personmanagement.entity.MbFollowupVisit>
     * @Date 2022/1/6 11:11
     * @author shikun
     */
    public List<MbFollowupVisit> getcalendarVisitCutDate(String id,String date){
        return mbFollowupVisitMapper.getcalendarVisitCutDate(id,date);
    }

    public List messageRemind(String userid) throws Exception{
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        String currentDate = simpleDateFormat.format(date);
        List<MbFollowupVisit> list =mbFollowupVisitMapper.selectList(queryWrapper);
        List<MbFollowupVisit> rList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            MbFollowupVisit mbFollowupVisit = list.get(i);
            String remindMinut[] = mbFollowupVisit.getRemindMinut().split(",");
            boolean temp = false;
            for (int j=0;j<remindMinut.length;j++){
                if(null != remindMinut[j] && !remindMinut[j].equals("")) {
                    Date minut1 = simpleDateFormat.parse(subOrAddMinut(-Integer.parseInt((remindMinut[j]))));
                    Date minut2 = simpleDateFormat.parse(mbFollowupVisit.getDate());
                    if (minut1.compareTo(minut2) >= 0) {
                        rList.add(mbFollowupVisit);
                        temp = true;
                        continue;
                    }
                }
            }
            if(temp){
                continue;
            }
            String remindTime[] = mbFollowupVisit.getRemindTime().split(",");
            for (int j=0;j<remindTime.length;j++){
                if(null != remindTime[j] && !remindTime[j].equals("")) {
                    Date time1 = simpleDateFormat.parse(subOrAddMinut(-Integer.parseInt((remindTime[j]))));
                    Date time2 = simpleDateFormat.parse(mbFollowupVisit.getDate());
                    if (time1.compareTo(time2) >= 0) {
                        rList.add(mbFollowupVisit);
                        temp = true;
                        continue;
                    }
                }
            }
            if(temp){
                continue;
            }
            String remindDay[] = mbFollowupVisit.getRemindDay().split(",");
            for (int j=0;j<remindDay.length;j++){
                if(null != remindDay[j] && !remindDay[j].equals("")) {
                    Date day1 = simpleDateFormat.parse(subOrAddMinut(-Integer.parseInt((remindDay[j]))));
                    Date day2 = simpleDateFormat.parse(mbFollowupVisit.getDate());
                    if (day1.compareTo(day2) >= 0) {
                        rList.add(mbFollowupVisit);
                        temp = true;
                        continue;
                    }
                }
            }
            if(temp){
                continue;
            }
        }
        return rList;
    }

    public List getWeekPlan(String userid,String level,String monday, String sunday,String type){
        System.out.println(monday+":::"+sunday+"::::"+type);
        List list =mbFollowupVisitMapper.getWeekPlan(userid, type, monday, sunday);
        return list;
    }

    public int changeVisit(int id){
        MbFollowupVisit mbFollowupVisit=new MbFollowupVisit();
        mbFollowupVisit.setId(id);
        mbFollowupVisit.setComplete(1);
        return mbFollowupVisitMapper.updateById(mbFollowupVisit);
    }

    public int delVisit(int id){
        return mbFollowupVisitMapper.deleteById(id);
    }

    public int editSave(MbFollowupVisit mbFollowupVisit){
        return mbFollowupVisitMapper.updateById(mbFollowupVisit);
    }


    public List getVisitToDay(String date){
        return mbFollowupVisitMapper.getVisitToDay(date);
    }


    public List gecunrenshu(String type, String userid){
        return mbFollowupVisitMapper.gecunrenshu(type, userid);
    }

}
